package MyFitnessPlate.Handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedWeightDataSet;

public class WeightHandler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================
	@SuppressWarnings("unused")
	private boolean in_xml = false;
	@SuppressWarnings("unused")
	private boolean in_entry = false;
	private boolean in_id = false;
	private boolean in_weight = false;
	private boolean in_month = false;
	private boolean in_day = false;
	private boolean in_year = false;
	private ArrayList<ParsedWeightDataSet> myParsedWeightDataSet;// = new ArrayList<ParsedWeightDataSet>(0);

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public ArrayList<ParsedWeightDataSet> getParsedData() {
		return this.myParsedWeightDataSet;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedWeightDataSet = new ArrayList<ParsedWeightDataSet>();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("id")) {
			this.in_id = true;
		} else if (localName.equals("entry")) {
			this.in_entry = true;
		} else if (localName.equals("weight")) {
			this.in_weight = true;
		} else if (localName.equals("month")) {
			this.in_month = true;
		} else if (localName.equals("day")) {
			this.in_day = true;
		} else if (localName.equals("year")) {
			this.in_year = true;
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("id")) {
			this.in_id = false;
		} else if (localName.equals("entry")) {
			this.in_entry = false;
		} else if (localName.equals("weight")) {
			this.in_weight = false;
		} else if (localName.equals("month")) {
			this.in_month = false;
		} else if (localName.equals("day")) {
			this.in_day = false;
		} else if (localName.equals("year")) {
			this.in_year = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_id) {
			myParsedWeightDataSet.add(new ParsedWeightDataSet());
			myParsedWeightDataSet.get(myParsedWeightDataSet.size()-1).setId(new String(ch, start, length).trim());
		}
		if (this.in_weight) {
			myParsedWeightDataSet.get(myParsedWeightDataSet.size()-1).setWeight(new String(ch, start, length).trim());
		}
		if (this.in_month) {
			myParsedWeightDataSet.get(myParsedWeightDataSet.size()-1).setMonth(new String(ch, start, length).trim());
		}
		if (this.in_day) {
			myParsedWeightDataSet.get(myParsedWeightDataSet.size()-1).setDay(new String(ch, start, length).trim());
		}
		if (this.in_year) {
			myParsedWeightDataSet.get(myParsedWeightDataSet.size()-1).setYear(new String(ch, start, length).trim());
		}
	}
}